package com.lanyou.esb.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.lanyou.cook.common.Constants;
import com.lanyou.cook.entity.domain.Field;
import com.lanyou.cook.util.DateUtil;
import com.lanyou.cook.web.param.FieldParam;
import com.lanyou.esb.entity.ServiceLogTrace;
import com.lanyou.esb.repository.ServiceLogTraceReppository;
import com.lanyou.esb.web.param.InterfaceParameterParam;
import com.lanyou.esb.web.param.ServiceLogTraceParam;

/**
 * 
 * @author 曾族根
 */
@RestController
@RequestMapping(Constants.BASE_URL_API + "/serviceLogTraces")
public class ServiceLogTraceController {

	@Autowired
	ServiceLogTraceReppository serviceLogTraceReppository;

	@RequestMapping("/search/findByExample")
	public Page<ServiceLogTrace> findByExample(ServiceLogTraceParam param, Pageable pageable){
		
		Specification<ServiceLogTrace> spec = new Specification<ServiceLogTrace>() {
			@Override
			public Predicate toPredicate(Root<ServiceLogTrace> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicatesList = new ArrayList<>();
				if(StringUtils.isNotBlank(param.getFieldName())){
					predicatesList.add(cb.equal((root.get("http").get("fieldSystem").get("field").get("id")),param.getFieldName()));
				}
				
				if(StringUtils.isNotBlank(param.getSystemName())){
					predicatesList.add(cb.equal((root.get("http").get("fieldSystem").get("id")),param.getSystemName()));
				}
				
				if(StringUtils.isNotBlank(param.getInterfaceName())){
					predicatesList.add(cb.like((root.get("http").get("name")),"%"+param.getInterfaceName()+"%"));
				}
				
				query.where(predicatesList.toArray(new Predicate[predicatesList.size()]));
				return null;
			}
		};
		return serviceLogTraceReppository.findAll(spec, pageable);
	}

}